<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":true},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="使用Travis平台的可持续集成服务来部署自己的Hexo博客。 环境说明Github公开项目(Travis对开源项目是免费，对私有项目是收费的)Linux服务器(这里用的阿里云的学生机，系统是CentOS 7.0)终端连接器（用的是Manjaro终端直接连接）">
<meta property="og:type" content="article">
<meta property="og:title" content="使用Travis部署Hexo">
<meta property="og:url" content="http://yoursite.com/2020/02/06/%E4%BD%BF%E7%94%A8Travis%E9%83%A8%E7%BD%B2Hexo/index.html">
<meta property="og:site_name" content="FU">
<meta property="og:description" content="使用Travis平台的可持续集成服务来部署自己的Hexo博客。 环境说明Github公开项目(Travis对开源项目是免费，对私有项目是收费的)Linux服务器(这里用的阿里云的学生机，系统是CentOS 7.0)终端连接器（用的是Manjaro终端直接连接）">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oss.chenjunxin.com/picture/blogPicture/b69cbc2b_travis_dashboard_hexo_setting.webp">
<meta property="article:published_time" content="2020-02-06T13:24:06.000Z">
<meta property="article:modified_time" content="2020-08-02T11:49:36.759Z">
<meta property="article:author" content="fu">
<meta property="article:tag" content="Travis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oss.chenjunxin.com/picture/blogPicture/b69cbc2b_travis_dashboard_hexo_setting.webp">

<link rel="canonical" href="http://yoursite.com/2020/02/06/%E4%BD%BF%E7%94%A8Travis%E9%83%A8%E7%BD%B2Hexo/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>使用Travis部署Hexo | FU</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">FU</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/02/06/%E4%BD%BF%E7%94%A8Travis%E9%83%A8%E7%BD%B2Hexo/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="fu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="FU">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          使用Travis部署Hexo
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-02-06 21:24:06" itemprop="dateCreated datePublished" datetime="2020-02-06T21:24:06+08:00">2020-02-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-08-02 19:49:36" itemprop="dateModified" datetime="2020-08-02T19:49:36+08:00">2020-08-02</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">工具</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B7%A5%E5%85%B7/Travis/" itemprop="url" rel="index"><span itemprop="name">Travis</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>使用Travis平台的可持续集成服务来部署自己的Hexo博客。</p>
<h1 id="环境说明"><a href="#环境说明" class="headerlink" title="环境说明"></a>环境说明</h1><p>Github公开项目(Travis对开源项目是免费，对私有项目是收费的)<br>Linux服务器(这里用的阿里云的学生机，系统是CentOS 7.0)<br>终端连接器（用的是Manjaro终端直接连接）</p>
<a id="more"></a>

<h1 id="自动化部署流程"><a href="#自动化部署流程" class="headerlink" title="自动化部署流程"></a>自动化部署流程</h1><ol>
<li>本地修改代码，提交到指定分支</li>
<li>Travis监听仓库改变</li>
<li>Travis执行install和script任务（这里可以做一些安装测试构建任务的依赖和测试构建命令。）</li>
<li>任务执行成功后，在Travis的 after_success 钩子里面用 ssh免密登陆 服务器</li>
<li>自动在服务器上执行配置的脚本</li>
</ol>
<h1 id="Travis项目配置"><a href="#Travis项目配置" class="headerlink" title="Travis项目配置"></a>Travis项目配置</h1><ol>
<li>在Github创建一个公有(public)仓库，然后访问官方网站travis-ci.org或者travis.com，推荐后一个，因为可以私有仓库（每个帐号一开始有100次私有仓库构建的体验服务）和公有仓库的构建都能一起管理，.org只能单看公有仓库的构建。点击右上角的个人头像，使用 Github 账户登入 Travis CI。<br>Travis 会列出 Github 上面你的所有仓库，以及你所属于的组织。此时，选择你需要 Travis 帮你构建的仓库，打开仓库旁边的开关。一旦激活了一个仓库，Travis 会监听这个仓库的所有变化。<br>假设现在已经对某个项目开启了 Travis，那么先去看看 Settings 里默认开启的那几项，根据自己实际需求进行设置，没什么特殊需求默认的设置就可以了。</li>
<li>创建添加 .travis.yml 到项目的根目录（稍后要提交到你关联的仓库中的）说白了接下来的事情都是如何去写这个配置文件，因为 Travis 全是根据这个配置文件去执行相应动作的。</li>
<li>在本地项目根目录的master分支里面添加 .travis.yml 配置文件，添加如下配置:<figure class="highlight yaml"><figcaption><span>.travis.yml</span><a target="_blank" rel="noopener" href="https://github.com/Johnnyww/hexo/blob/master/.travis.yml">链接地址</a></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">language:</span> <span class="string">node_js</span>   <span class="comment">#前端工程所以是JavaScript，编译环境是node_js</span></span><br><span class="line"><span class="attr">node_js:</span></span><br><span class="line"><span class="bullet">-</span> <span class="number">12.14</span><span class="number">.1</span>   <span class="comment">#指定node版本</span></span><br><span class="line"><span class="attr">branchs:</span></span><br><span class="line">  <span class="attr">only:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">master</span>  <span class="comment">#指定只有检测到master分支有变动时才执行任务</span></span><br></pre></td></tr></table></figure>
将该配置文件推送到服务器。 返回Travis网站，此时Travis已经检测到该仓库的变动（有可能会有点延迟），所以会根据仓库根目录下的.travis.yml配置文件开始执行任务。<br>可以在Job log界面看任务执行时的输出，在View config的标签页下面看到此次任务的配置信息，包括了你在.travis.yml里面配置的和Travis自动配置的信息。<br>此时，Travis监听仓库变化，自动执行任务已经完成了，也就是流程中的前三步已经完成了。</li>
</ol>
<h1 id="Travis使用SSH免密登陆服务器"><a href="#Travis使用SSH免密登陆服务器" class="headerlink" title="Travis使用SSH免密登陆服务器"></a>Travis使用SSH免密登陆服务器</h1><p>SSH的登陆原理看这个<a target="_blank" rel="noopener" href="https://www.cnblogs.com/scofi/p/6617394.html">SSH公钥登陆原理</a>。SSH录利用的是非对称加密的方式。基本步骤就是</p>
<ul>
<li>在客户端生成公钥和私钥</li>
<li>然后将私钥保存在客户端的~/.ssh/id_rsa中</li>
<li>将公钥保存在服务器的~/.ssh/authorized_keys文件中<br>更改文件的权限后，我们就能直接通过ssh免密登录我们的服务器。但是我们是无法操作travis的客户端的，但是如果我们了解ssh免密登录的原理后，你会发现公钥和私钥在什么地方生成都行，最重要的是两个要成对。</li>
</ul>
<p>ssh免密登录的大致过程就是：客户端向服务器发起登录请求，服务器向客户端返回一段随机字符串，客户端收到这段字符串后使用自己的私钥进行加密，然后发给服务器。服务器接收后使用自己保存的公钥解密，如果解密后的数据相等就登录成功，否则断开连接。</p>
<p>所以，公钥和私钥的生成我们完全可以在自己的服务器上进行操作。首先，登录到自己的服务器。</p>
<h2 id="生成公钥-私钥对"><a href="#生成公钥-私钥对" class="headerlink" title="生成公钥/私钥对"></a>生成公钥/私钥对</h2><p>这里直接在服务器上面生成密钥对，理论上在哪里生成密钥对都可以，只要是配套的。<br>用一个非root的常用用户或者新建一个用户，切到该用户下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成RSA密钥对，后面所有的直接以默认就行，passphase一定要为空</span></span><br><span class="line">$ ssh-keygen -t rsa</span><br></pre></td></tr></table></figure>
<p>可以看到生成密钥对在用户家目录的.ssh文件夹下面。 由于Linux权限的控制规则，文件的权限不是越大越好，所有需要设置合适的权限。这里需要把.ssh目录设置为700权限（可能默认就是），目录下面的文件设置为600权限。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chmod 600 ~/.ssh/*</span><br></pre></td></tr></table></figure>
<p>然后将生成的公钥(id_rsa.pub)中的内容添加到.ssh/authorized_keys中</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ touch ~/.ssh/authorized_keys</span><br><span class="line">$ cat ~/.ssh/id_rsa.pub &gt;&gt;  ~/.ssh/authorized_keys</span><br></pre></td></tr></table></figure>
<p>到此，密钥对已经生成完成。下面，我们就需要测试一下免密登录的操作是否能成功。在.ssh目录下执行一下操作：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ touch config</span><br></pre></td></tr></table></figure>
<p>然后在config中添加如下内容:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Host <span class="built_in">test</span></span><br><span class="line"><span class="comment"># 换成需要登录的主机ip</span></span><br><span class="line">HostName <span class="variable">$IP</span></span><br><span class="line"><span class="comment"># 端口号</span></span><br><span class="line">Port 6666</span><br><span class="line"><span class="comment"># 换成需要登录的用户名</span></span><br><span class="line">User <span class="variable">$UserName</span></span><br><span class="line">StrictHostKeyChecking no</span><br><span class="line">IdentitiesOnly yes</span><br><span class="line"><span class="comment"># 私钥的存放路径</span></span><br><span class="line">IdentityFile ~/.ssh/id_rsa</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ ssh <span class="built_in">test</span></span><br></pre></td></tr></table></figure>
<p>运行ssh test后报错Bad owner or permissions on ~/.ssh/config。这里同样要求我们要把.ssh/config和.ssh/authorized_keys权限设置为600。登录成功的花会有另外的信息，同时在.ssh/下多了一个known_hosts文件。</p>
<p>下面我们就需要将上面的密钥对中的私钥放在travis上，但是我们是无法直接操作travis服务器的，因此我们也就不能将私钥放在travis服务器上。但是，我们可以将私钥放在travis服务器能够读取的地方，因为travis是能够直接读取我们项目的工程的，所以，我们可以将私钥放在我们的项目中，travis提供了对私钥进行加密的功能，我们可以将私钥加密之后放在我们的工程中，当travis需要连接我们的服务器时，就解密这个私钥用于连接。</p>
<h2 id="安装Travis客户端"><a href="#安装Travis客户端" class="headerlink" title="安装Travis客户端"></a>安装Travis客户端</h2><h3 id="安装ruby"><a href="#安装ruby" class="headerlink" title="安装ruby"></a>安装ruby</h3><p>Travis是通过gem进行安装的，<code>gem</code>是ruby的管理工具，所以我们需要先安装ruby。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ curl -sSL https://get.rvm.io | bash -s stable</span><br><span class="line">sudo yum install ruby <span class="comment">#这是centos系统下安装命令</span></span><br><span class="line">$ yum install rubygems</span><br><span class="line">$ yum install ruby-devel</span><br></pre></td></tr></table></figure>
<p>查看RubyGems环境变量配置命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">which</span> gem</span><br></pre></td></tr></table></figure>
<p>正确找到会输出内容 /usr/bin/gem，如果没有找，则需要进行环境变量的设置，有则无需设置了。</p>
<h3 id="修改镜像源"><a href="#修改镜像源" class="headerlink" title="修改镜像源"></a>修改镜像源</h3><p>安装完ruby之后就可以使用gem包管理工具了，但是好像官方镜像源被墙了，所以需要更换gem的镜像源。参考<a target="_blank" rel="noopener" href="https://gems.ruby-china.com/">Ruby China</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">$ gem sources -l</span><br><span class="line">*** CURRENT SOURCES ***</span><br><span class="line"></span><br><span class="line">https://rubygems.org/</span><br><span class="line">$ gem -v</span><br><span class="line">2.7.7</span><br><span class="line"><span class="comment">#更换镜像源</span></span><br><span class="line">$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/</span><br><span class="line">https://gems.ruby-china.com/ added to sources</span><br><span class="line">https://rubygems.org/ removed from sources</span><br><span class="line">$ gem sources -l</span><br><span class="line">*** CURRENT SOURCES ***</span><br><span class="line"></span><br><span class="line">https://gems.ruby-china.com/</span><br></pre></td></tr></table></figure>

<h3 id="安装Travis命令行工具"><a href="#安装Travis命令行工具" class="headerlink" title="安装Travis命令行工具"></a>安装Travis命令行工具</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ gem install travis</span><br><span class="line">$ travis <span class="built_in">help</span> <span class="comment">#测试travis客户端是否安装成功</span></span><br></pre></td></tr></table></figure>
<p>将项目用git克隆到服务器上，进入到项目的根目录下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ travis login</span><br><span class="line">或者运行以下其中一条命令，根据GitHub关联授权的travis网址后缀选择</span><br><span class="line">$ travis login --com</span><br><span class="line">$ travis login --org</span><br><span class="line"><span class="comment"># 加密私钥匙并把的对应信息加入到.travis.yml中</span></span><br><span class="line">$ travis encrypt-file ~/.ssh/id_rsa  --add</span><br></pre></td></tr></table></figure>
<p>然后，你就会发现在项目的根目录下多了一个id_rsa.enc文件以及.travis.yml多了一个before_install指令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ cat .travis.yml</span><br><span class="line">language: node_js</span><br><span class="line">node_js:</span><br><span class="line">- 12.14.1</span><br><span class="line">branchs:</span><br><span class="line">  only:</span><br><span class="line">  - master</span><br><span class="line">before_install:</span><br><span class="line">- openssl aes-256-cbc -K <span class="variable">$encrypted_311a3ff6a6ef_key</span> -iv <span class="variable">$encrypted_311a3ff6a6ef_iv</span></span><br><span class="line">  -<span class="keyword">in</span> id_rsa.enc -out ~/.ssh/id_rsa -d</span><br></pre></td></tr></table></figure>
<p>解密命令中 -in 和 -out 参数:</p>
<ul>
<li>in 参数指定待解密的文件，位于仓库的根目录(Travis执行任务时会先把代码拉到Travis自己的服务器上，并进入仓库更目录)</li>
<li>out 参数指定解密后的密钥存放在Travis服务器的<code>~/.ssh/id_rsa</code>,需要手动将-out选项的值多了的一个\符号去掉</li>
</ul>
<p>然后将变更推送到master分支，触发travis构建。</p>
<h1 id="配置after-success"><a href="#配置after-success" class="headerlink" title="配置after_success"></a>配置after_success</h1><p>到此，我们就差最后一个步骤了：在.travis.yml中添加一些配置，主要是after_success钩子配置。修改之后的配置如下：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">language:</span> <span class="string">node_js</span></span><br><span class="line"><span class="attr">node_js:</span></span><br><span class="line"><span class="bullet">-</span> <span class="number">12.14</span><span class="number">.1</span></span><br><span class="line"><span class="attr">branchs:</span></span><br><span class="line">  <span class="attr">only:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">master</span>  <span class="comment">#设定分支</span></span><br><span class="line"><span class="attr">addons:</span></span><br><span class="line">  <span class="attr">ssh_known_hosts:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">$SERVER_IP</span>  <span class="comment">#受信主机，在使用 ssh 登陆时会确认主机信息，travis-ci 无法进行交互操作。所以需要在 .travis.yml 中添加addons:ssh_known_hosts</span></span><br><span class="line"><span class="attr">before_install:</span></span><br><span class="line"><span class="bullet">-</span> <span class="string">openssl</span> <span class="string">aes-256-cbc</span> <span class="string">-K</span> <span class="string">$encrypted_311a3ff6a6ef_key</span> <span class="string">-iv</span> <span class="string">$encrypted_311a3ff6a6ef_iv</span></span><br><span class="line">  <span class="string">-in</span> <span class="string">id_rsa.enc</span> <span class="string">-out</span> <span class="string">~/.ssh/id_rsa</span> <span class="string">-d</span></span><br><span class="line"><span class="bullet">-</span> <span class="string">chmod</span> <span class="number">600</span> <span class="string">~/.ssh/id_rsa</span> <span class="comment">#还是Linux文件权限问题</span></span><br><span class="line"><span class="bullet">-</span> <span class="string">echo</span> <span class="string">-e</span> <span class="string">&quot;Host &quot;</span><span class="string">$SERVER_IP&quot;\n\tStrictHostKeyChecking</span> <span class="literal">no</span><span class="string">\n&quot;</span> <span class="string">&gt;&gt;</span> <span class="string">~/.ssh/config</span></span><br><span class="line"><span class="attr">after_success:</span></span><br><span class="line"><span class="bullet">-</span> <span class="string">ssh</span> <span class="string">chenjunxin@&quot;$SERVER_IP&quot;</span> <span class="string">-p</span> <span class="string">&quot;$SERVER_PORT&quot;</span> <span class="string">-o</span> <span class="string">StrictHostKeyChecking=no</span> <span class="string">&#x27;. /home/chenjunxin/deploy/deployhexo.sh&#x27;</span></span><br><span class="line"><span class="comment"># - ssh chenjunxin@&quot;$SERVER_IP&quot; -o StrictHostKeyChecking=no &#x27;cd ~/hexo &amp;&amp; git pull &amp;&amp; npm install &amp;&amp; hexo clean &amp;&amp; hexo g&#x27;</span></span><br></pre></td></tr></table></figure>
<p>ssh命令需要加上StrictHostKeyChecking=no选项，具体的可以查看禁用ssh远程主机的公钥检查。<br>可以直接在ssh连接后面写要运行的命令，也可以写在服务器上的shell文件里，然后远程登录执行。<br>after_success是在Travis执行完 install 和 script 之后执行的钩子,其他的Travis配置可以参考官方文档。<br>“$SERVER_IP”:这个值是Travis加密的值，可以设置在Job log中是否显示。可以在Travis控制台中的Setting中设置。<br><img src="https://oss.chenjunxin.com/picture/blogPicture/b69cbc2b_travis_dashboard_hexo_setting.webp"><br>也可以在服务器上运行以下命令（$IP填具体IP的值）</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ travis encrypt  <span class="string">&quot;SERVER_IP=<span class="variable">$IP</span>&quot;</span> --add</span><br></pre></td></tr></table></figure>
<p>命令执行之后会在.travis.yml新增一个sercure的值。<br>在运行的时候，Travis那边的服务器会分别从Setting设置与.travis.yml文件获取定义的变量值。更详细的内容可以查阅<a target="_blank" rel="noopener" href="https://docs.travis-ci.com/user/environment-variables/">官方文档</a></p>
<h1 id="一点补充"><a href="#一点补充" class="headerlink" title="一点补充"></a>一点补充</h1><p>服务器上的travis客户端的配置文件在用户目录下的<code>$HOME/.travis/</code>中。其中的config。yml里可以 endpoint可以配置数据库构建的地址，可以是<a target="_blank" rel="noopener" href="https://api.travis-ci.com/">https://api.travis-ci.com/</a> 或者 <a target="_blank" rel="noopener" href="https://api.travis-ci.com/">https://api.travis-ci.com/</a></p>
<h1 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h1><ul>
<li><a target="_blank" rel="noopener" href="https://www.itfanr.cc/2017/08/09/using-travis-ci-automatic-deploy-hexo-blogs/">使用Travis CI自动部署Hexo博客</a></li>
<li><a target="_blank" rel="noopener" href="https://www.dazhuanlan.com/2019/10/15/5da588149783f/">travis自动化部署github项目到server</a></li>
<li><a target="_blank" rel="noopener" href="https://changkun.us/archives/2017/06/232/">Hexo + GitHub + Travis CI + VPS 自动部署</a></li>
<li><a target="_blank" rel="noopener" href="https://www.jonhuu.com/sample-post/1108.html">Travis CI 系列自动化部署测试教程（VPS服务器）</a></li>
<li><a target="_blank" rel="noopener" href="https://andyliwr.github.io/2018/04/28/travis_node_publish2/">travis自动化部署续篇</a></li>
<li><a target="_blank" rel="noopener" href="https://juejin.im/post/5a9e1a5751882555712bd8e1">Travis-CI自动化测试并部署至自己的CentOS服务器</a></li>
<li><a target="_blank" rel="noopener" href="https://cnodejs.org/topic/5885f19c171f3bc843f6017e">利用travis-ci持续部署nodejs应用</a></li>
<li><a target="_blank" rel="noopener" href="http://www.worldhello.net/2010/04/08/1026.html">禁用SSH远程主机的公钥检查</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/bingguang1993/article/details/82415543">解决linux中ssh登录Warning:Permanently added (RSA) to the list of known hosts</a></li>
<li><a target="_blank" rel="noopener" href="http://www.cocoachina.com/articles/24904">TrueChain持续集成项目打包（Travis-CI）</a></li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Travis/" rel="tag"># Travis</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/02/03/Manjaro%E6%90%AD%E5%BB%BASamba%E5%92%8CDLNA%E6%9C%8D%E5%8A%A1/" rel="prev" title="Manjaro搭建Samba和DLNA服务">
      <i class="fa fa-chevron-left"></i> Manjaro搭建Samba和DLNA服务
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/03/01/Manjaro%E5%88%A9%E7%94%A8AUR%E8%A3%85%E9%99%8D%E7%BA%A7%E8%BD%AF%E4%BB%B6%E5%8C%85/" rel="next" title="Manjaro利用AUR装降级软件包">
      Manjaro利用AUR装降级软件包 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%8E%AF%E5%A2%83%E8%AF%B4%E6%98%8E"><span class="nav-text">环境说明</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E5%8C%96%E9%83%A8%E7%BD%B2%E6%B5%81%E7%A8%8B"><span class="nav-text">自动化部署流程</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Travis%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE"><span class="nav-text">Travis项目配置</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Travis%E4%BD%BF%E7%94%A8SSH%E5%85%8D%E5%AF%86%E7%99%BB%E9%99%86%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="nav-text">Travis使用SSH免密登陆服务器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%94%9F%E6%88%90%E5%85%AC%E9%92%A5-%E7%A7%81%E9%92%A5%E5%AF%B9"><span class="nav-text">生成公钥&#x2F;私钥对</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%89%E8%A3%85Travis%E5%AE%A2%E6%88%B7%E7%AB%AF"><span class="nav-text">安装Travis客户端</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%89%E8%A3%85ruby"><span class="nav-text">安装ruby</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BF%AE%E6%94%B9%E9%95%9C%E5%83%8F%E6%BA%90"><span class="nav-text">修改镜像源</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%89%E8%A3%85Travis%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%B7%A5%E5%85%B7"><span class="nav-text">安装Travis命令行工具</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%85%8D%E7%BD%AEafter-success"><span class="nav-text">配置after_success</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E7%82%B9%E8%A1%A5%E5%85%85"><span class="nav-text">一点补充</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E9%93%BE%E6%8E%A5"><span class="nav-text">参考链接</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">fu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">9</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yourname" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/yourname" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">fu</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

</body>
</html>
